package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.result.PageResult;
import com.sky.service.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import com.sky.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    AddressBookMapper addressBookMapper;
    @Autowired
    ShopCartMapper shopCartMapper;
    @Autowired
    OrderMapper orderMapper;


    @Override
    public PageResult conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(),ordersPageQueryDTO.getPageSize());
        Page<OrderVO> page= orderMapper.pageQuery(ordersPageQueryDTO);
        page.forEach((item)->{
            List<OrderDetail> orderDetails = orderMapper.queryDetailByOrderId(item.getId());
            item.setOrderDetailList(orderDetails);
        });
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public OrderVO orderDetail(Long id) {
        OrderVO orderVO=orderMapper.queryById(id);
        List<OrderDetail> orderDetails=orderMapper.queryDetailByOrderId(id);
        orderVO.setOrderDetailList(orderDetails);
        return orderVO;
    }

    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) {
        Orders orders=Orders.builder().id(ordersCancelDTO.getId()).status(6).cancelReason(ordersCancelDTO.getCancelReason()).build();
        orderMapper.update(orders);
    }

    @Override
    public OrderStatisticsVO statistics() {
        Integer confirmed= orderMapper.queryByStatus(3);
        Integer deliveryInProgress= orderMapper.queryByStatus(4);
        Integer toBeConfirmed= orderMapper.queryByStatus(2);
        OrderStatisticsVO orderStatisticsVO=new OrderStatisticsVO();
        orderStatisticsVO.setConfirmed(confirmed);
        orderStatisticsVO.setDeliveryInProgress(deliveryInProgress);
        orderStatisticsVO.setToBeConfirmed(toBeConfirmed);

        return orderStatisticsVO;
    }

    @Override
    public void confirm(Long id) {
        Orders orders=Orders.builder().id(id).status(3).build();
        orderMapper.update(orders);
    }

    @Override
    public void rejection(OrdersRejectionDTO ordersRejectionDTO) {
        Orders orders=Orders.builder().id(ordersRejectionDTO.getId()).status(6).rejectionReason(ordersRejectionDTO.getRejectionReason()).build();
        orderMapper.update(orders);
    }

    @Override
    public void delivery(Long id) {
        Orders orders=Orders.builder().id(id).status(4).build();
        orderMapper.update(orders);
    }

    @Override
    public void complete(Long id) {
        Orders orders=Orders.builder().id(id).status(5).build();
        orderMapper.update(orders);
    }
}
